package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.common.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @ClassName : com.atguigu.springcloud.controller.PaymentController
 * @Description : 类描述
 * Created by cyd on 2021-09-13 14:59:40
 */
@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping(value = "/creat")
    public CommonResult<Integer> creatPayment(@RequestBody Payment payment) {
        int result = paymentService.creat(payment);
        log.info("数据库插入行数------{}", result);
        if (result > 0) {
            return new CommonResult<Integer>(200, "success,serverPort:" + serverPort, result);
        } else {
            return new CommonResult<Integer>(444, "fail,serverPort:" + serverPort, result);
        }
    }

    @GetMapping(value = "/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id) {
        Payment payment = paymentService.getPayment(id);
        log.info("查询结果---{}", payment);
        if (null != payment) {
            return new CommonResult<Payment>(200, "success,serverPort:" + serverPort, payment);
        } else {
            return new CommonResult<>(444, "fail,serverPort:" + serverPort, null);
        }

    }

    @GetMapping(value = "/lb")
    public CommonResult<Payment> getPort() {
        return new CommonResult<>(200, serverPort);
    }

}
